package com.liyao.windowtest4.Utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.*;

/**
 * @program: windowtest4
 * @description:
 * @author: liu yan
 * @create: 2024-11-01 18:02
 */
public class ReadTxt {
    private String sn;
    private String host;
    private int port;

    public String getSn() {
        return sn;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public boolean isLoop() {
        return loop;
    }

    public String getJobID() {
        return jobID;
    }

    public JSONArray getJobContent() {
        return jobContent;
    }

    public synchronized String getRoutineName() {
        return RoutineName;
    }

    private boolean loop=false;
    private String jobID;
    private JSONArray jobContent;
    private String RoutineName;

    private String configDirPath = System.getProperty("user.dir") + File.separator + "config" + File.separator;

    public ReadTxt(String SN){
        this.sn=SN;

        //获取扳手的host、port
        try (BufferedReader br = new BufferedReader(new FileReader(configDirPath + "torqueItems.txt"))) {
//        try{
//            InputStream inputStream = getClass().getClassLoader().getResourceAsStream("torqueItems.txt");
            if(br==null){
                System.err.println("未找到系统文件：torqueItems.txt");
            }
//            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = br.readLine()) != null) {
                if(line.contains("\"SN\":\"" + sn + "\"")){
                    JSONObject js= JSONObject.fromObject(line);
                    this.host=js.getString("Host");
                    this.port=js.getInt("Port");
                    System.out.println("地址："+host+",端口号："+port);
                    break;
                }
            }
        } catch (IOException e) {
            System.out.println("获取Host和Port失败");
            e.printStackTrace();
        }

        //获取任务是否无限循环
                try (BufferedReader br = new BufferedReader(new FileReader(configDirPath+"taskItems.txt"))) {
//        try{
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream("taskItems.txt");
            if(br == null){
                System.err.println("为获取到系统文件：taskItems.txt");
            }
//            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = br.readLine()) != null) {
                if(line.contains("\"SN\":\"" + sn + "\"") && line.contains("\"pattern\":\"循环\"")){
                    this.loop=true;
                    JSONObject js=JSONObject.fromObject(line);
                    this.jobID=js.getString("RoutineId");
                    break;
                }
            }
        } catch (IOException e) {
            System.out.println("获取是否循环失败");
            e.printStackTrace();
        }

        //获取job的list
        try (BufferedReader br = new BufferedReader(new FileReader(configDirPath+"Routine.txt"))) {
//        try{
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream("Routine.txt");
            if(br == null){
                System.err.println("为获取到系统文件：Routine.txt");
            }
//            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = br.readLine()) != null) {
                if(line.contains("\"code\":\"" + this.jobID + "\"")){
                    JSONObject js= JSONObject.fromObject(line);
                    this.RoutineName=js.getString("RoutineName");
                    this.jobContent= JSONArray.fromObject(js.getString("content"));
                    System.out.println("程序名称："+RoutineName+",任务详情："+jobContent);
                    break;
                }
            }
        } catch (IOException e) {
            System.out.println("获取job失败");
            e.printStackTrace();
        }
    }

}
